package com.practice.springboot_01_zwj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.practice.springboot_01_zwj.entity.AccountInfo;
import com.practice.springboot_01_zwj.entity.CloudNetwork;
import com.practice.springboot_01_zwj.mapper.AccountInfoMapper;
import com.practice.springboot_01_zwj.service.AccountInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: AccountInfoServiceImpl <br/>
 * Description: 针对账号信息操作的业务实现类 <br/>
 * date: 2024/1/21 15:04 <br/>
 * @author mky <br/>
 * @version v1.0 <br/>
 * @since JDK 17 <br/>
 */
@Service
public class AccountInfoServiceImpl implements AccountInfoService {
    @Autowired
    private AccountInfoMapper accountInfoMapper;


    @Override
    public List<AccountInfo> selectAll(String acName) {
        String searchTerm = "%" + acName + "%";
        return accountInfoMapper.selectAll(searchTerm);
    }

    @Override
    public List<AccountInfo> selectCityName(String cityName) {
        String searchTerm = "%" + cityName + "%";
        return accountInfoMapper.selectCityName(searchTerm);
    }

    @Override
    public int addAccountInfo(AccountInfo accountInfo) {
        return accountInfoMapper.addAccountInfo(accountInfo);
    }

    @Override
    public int updateAccountInfo(AccountInfo accountInfo) {
        return accountInfoMapper.updateAccountInfo(accountInfo);
    }

    @Override
    public int deleteAccountInfo(Integer userId) {
        return accountInfoMapper.deleteAccountInfo(userId);
    }

    @Override
    public PageInfo<AccountInfo> selectAcNameAndUserName(String acName, String userName,String cityName, int pageNum, int pageSize) {
        // 1. 参数校验
        if (pageNum < 1) pageNum = 1;
        if (pageSize < 1 || pageSize > 100) pageSize = 10;

        // 2. 确保在查询前启动分页（这是关键步骤）
        PageHelper.startPage(pageNum, pageSize);

        // 3. 处理空字符串情况
        if (acName != null && acName.isEmpty()) {
            acName = null;
        }
        if (userName != null && userName.isEmpty()) {
            userName = null;
        }

        // 4. 执行查询（必须在 startPage 之后立即调用）
        List<AccountInfo> networks = accountInfoMapper.selectAcNameAndUserName(acName, userName,cityName);

        // 5. 使用 PageInfo 包装结果
        return new PageInfo<>(networks);
    }
}
